/**
 * 
 */
package com.faxsun.core.persist.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.faxsun.core.pojos.enums.TagType;

/**
 * @author langhsu
 * 
 */
@Entity
@Table(name = "fx_tags")
public class TagPO {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "tag_id")
	private long id;

	@Column(name = "name", unique = true)
	private String name;

	@Column(name = "last_post_id", columnDefinition = "bigint(20) default 0")
	private long lastPostId;

	private int featured; // 是否推荐，用于展示排序和推荐

	private int posts; // 文章数
	private int hots; // 热度，根据浏览记录计算得到的产品热度，暂未试用
	
	/**
	 * 评论数
	 * @author songdragon 2015-04-28 18:02
	 */
	private int comments;

	/**
	 * 不同类型的标签 0:普通 1:分类 2:产品
	 */
	@Column(name = "type", nullable = false, columnDefinition = "tinyint not null default 0")
	private int type;

	@Column(name = "is_deleted", length = 1, nullable = false, columnDefinition = "tinyint not null default 0")
	private int isDeleted;

	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
	@JoinTable(name = "fx_post_tag_ref", joinColumns = { @JoinColumn(name = "tag_id") }, inverseJoinColumns = { @JoinColumn(name = "post_id") })
	private Set<PostPO> postSet;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "product_id")
	private ProductRefPO productRef;
	
	/**
	 * Category Tag使用此字段，存储电商中的 Category id 信息：
	 * 若电商端Category id未－1，则为资讯自身的分类tag
	 */
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="category_id")
	private CategoryTagRefPO categoryRef;
	
	@Column(name="is_nav",columnDefinition="bit(1) not null default b'0'")
	private boolean isNav;
	
	/**
	 * 商品tag所属的分类tag
	 */
	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
	@JoinTable(name = "fx_categoryTag_tag_ref", joinColumns = { @JoinColumn(name = "tag_id") }, inverseJoinColumns = { @JoinColumn(name = "categoryTag_id") })
	private Set<TagPO> tagsCategorySet;

    @Column(name = "video_url", length = 1024)
    private String videoUrl;
    
    /**
     * 导航tag的英文名称，用于动画显示效果
     */
    @Column(name="nav_name",length=30)
    private String tagNavName;
 
    /**
     * 导航标签在哪个模块显示，采用按位或,01(1)：资讯；10(2)：评测，11(3):both
     */
    @Column(name="module")
    private int module;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPosts() {
		return posts;
	}

	public void setPosts(int posts) {
		this.posts = posts;
	}

	public int getHots() {
		return hots;
	}

	public void setHots(int hots) {
		this.hots = hots;
	}

	public long getLastPostId() {
		return lastPostId;
	}

	public void setLastPostId(long lastPostId) {
		this.lastPostId = lastPostId;
	}

	public int getFeatured() {
		return featured;
	}

	public void setFeatured(int featured) {
		this.featured = featured;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public TagType getEnumType() {
		return TagType.parse(type);
	}

	public void setEnumType(TagType tagType) {
		this.type = tagType.getValue();
	}

	public ProductRefPO getProductRef() {
		return productRef;
	}

	public void setProductRef(ProductRefPO productRef) {
		this.productRef = productRef;
	}

	public int getIsDeleted() {
		return isDeleted;
	}

	public void setIsDeleted(int isDeleted) {
		this.isDeleted = isDeleted;
	}

	public Set<PostPO> getPostSet() {
		return postSet;
	}

	public void setPostSet(Set<PostPO> postSet) {
		this.postSet = postSet;
	}

	public CategoryTagRefPO getCategoryRef() {
		return categoryRef;
	}

	public void setCategoryRef(CategoryTagRefPO categoryRef) {
		this.categoryRef = categoryRef;
	}

	public Set<TagPO> getTagsCategorySet() {
		if(tagsCategorySet==null){
			tagsCategorySet=new HashSet<TagPO>();
		}
		return tagsCategorySet;
	}

	public void setTagsCategorySet(Set<TagPO> tagsCategorySet) {
		this.tagsCategorySet = tagsCategorySet;
	}

	public int getComments() {
		return comments;
	}

	public void setComments(int comments) {
		this.comments = comments;
	}

	public boolean getIsNav() {
		return isNav;
	}

	public void setIsNav(boolean isNav) {
		this.isNav = isNav;
	}

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

	public String getTagNavName() {
		return tagNavName;
	}

	public void setTagNavName(String tagNavName) {
		this.tagNavName = tagNavName;
	}

	public void setNav(boolean isNav) {
		this.isNav = isNav;
	}

	public int getModule() {
		return module;
	}

	public void setModule(int module) {
		this.module = module;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TagPO other = (TagPO) obj;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "TagPO [id=" + id + ", name=" + name + ", lastPostId=" + lastPostId + ", featured=" + featured
				+ ", posts=" + posts + ", hots=" + hots + ", comments=" + comments + ", type=" + type + ", isDeleted="
				+ isDeleted + ", postSet=" + postSet + ", productRef=" + productRef + ", categoryRef=" + categoryRef
				+ ", isNav=" + isNav + ", tagsCategorySet=" + tagsCategorySet + ", videoUrl=" + videoUrl
				+ ", tagNavName=" + tagNavName + ", module=" + module + "]";
	}
		
}
